/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipti.br.tag.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import org.ipti.br.tag.util.EncryptionUtil;


@Entity
public class PerformanceFrequency extends AbstractEntity{

    @ManyToOne
    @JoinColumn(name = "actor_id")
    private Actor actor;

    @ManyToOne
    @JoinColumn(name = "plan_id")    
    private Plan plan;

    @Column(precision = 2)
    @Digits(integer = 2, fraction = 2, message="Valor deve ter no máximo 4 dígitos (ex: 10.00)")
    @DecimalMax(value="10.00", message="Valor deve ser menor ou igual a 10.00")
    @DecimalMin(value="0.00", message="Valor deve ser maior ou igual a 0.00")
    private float grade;
    
    @Column(precision = 2)
    @Digits(integer = 3, fraction = 2, message="Valor deve ter no máximo 5 dígitos (ex: 100.00)")
    @DecimalMax(value="100.00", message="Valor deve ser menor ou igual a 100.00")
    @DecimalMin(value="0.00", message="Valor deve ser maior ou igual a 0.00")
    private float frequency;
    
    public PerformanceFrequency() {
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String hash = ""
                    + ((this.getActor() != null) ? this.getActor().getId() : null)
                    + ((this.getPlan() != null) ? this.getPlan().getId() : null)
                    + this.getGrade()
                    + this.getFrequency();
            this.setId(Long.valueOf(EncryptionUtil.encrypt(hash).hashCode()));
        }
    }

    public PerformanceFrequency(Actor actor, Plan plan, float grade, float frequency) {
        this.actor = actor;
        this.plan = plan;
        this.grade = grade;
        this.frequency = frequency;
    }

    public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }

    public Plan getPlan() {
        return plan;
    }

    public void setPlan(Plan plan) {
        this.plan = plan;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }

    public float getFrequency() {
        return frequency;
    }

    public void setFrequency(float frequency) {
        this.frequency = frequency;
    }
}
